package com.bocloud.cmp.driver.providers.impl;

import org.springframework.stereotype.Service;

import com.bocloud.cmp.driver.config.Configuration;
import com.bocloud.cmp.driver.providers.SecurityGroupDriver;
import com.bocloud.cmp.driver.providers.ali.AliSecurityGroupDriver;
import com.bocloud.cmp.driver.providers.intf.SecurityGroupProvider;
import com.bocloud.cmp.driver.providers.jd.JdSecurityGroupDriver;
import com.bocloud.cmp.driver.utils.VendorConverter;
import com.bocloud.cmp.enums.VendorType;
import com.bocloud.cmp.model.Butler;
import com.bocloud.cmp.model.SecurityGroupModel;
import com.bocloud.common.model.BsmResult;

@Service("securityGroupProvider")
public class SecurityGroupProviderImpl implements SecurityGroupProvider {

	@Override
	public BsmResult list(String vendor, String region) {
		SecurityGroupDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdSecurityGroupDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			break;
		case ALIYUN:
			driver = new AliSecurityGroupDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			break;
		case OPENSTACK:
			break;
		case VMWARE:
			break;
		default:
			break;
		}
		if (null == driver) {
			return new BsmResult(false, "暂不支持！");
		} else if (!driver.accessable()) {
			return new BsmResult(false, "用户鉴权失败！");
		} else {
			return driver.list();
		}
	}

	@Override
	public BsmResult create(String vendor, String region, SecurityGroupModel group) {
		BsmResult result = null;
		SecurityGroupDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdSecurityGroupDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			result = driver.create(group);
			break;
		case ALIYUN:
			driver = new AliSecurityGroupDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			result = driver.create(group);
			break;
		case OPENSTACK:
			break;
		case VMWARE:
			break;
		default:
			break;
		}
		return result;
	}

	@Override
	public BsmResult remove(String sgId, String vendor, String region) {
		BsmResult result = null;
		SecurityGroupDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdSecurityGroupDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			result = driver.remove(sgId);
			break;
		case ALIYUN:
			driver = new AliSecurityGroupDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			result = driver.remove(sgId);
			break;
		case OPENSTACK:
			break;
		case VMWARE:
			break;
		default:
			break;
		}
		return result;
	}

	@Override
	public BsmResult detail(String sgId, String vendor, String region) {
		BsmResult result = null;
		SecurityGroupDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdSecurityGroupDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			result = driver.detail(sgId);
			break;
		case ALIYUN:
			driver = new AliSecurityGroupDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			result = driver.detail(sgId);
			break;
		case OPENSTACK:
			break;
		case VMWARE:
			break;
		default:
			break;
		}
		return result;
	}

}
